"""热门搜索词 API Router"""
from typing import List
from typing_extensions import Annotated

from fastapi import APIRouter, Depends, Request

from api.v1.service.popular import PopularServiceAPI
from api.v1.models.popular import (
    CreatePopularRequest, PopularRequest, PopularResponse
)
from api.v1.models.response import MessageResponse, response_409, response_422, response_500
from service.popular_service import PopularService


router = APIRouter()


@router.get("/populars",
            summary="热门搜索列表",
            responses={**response_422, **response_500})
async def read_populars(
    service: Annotated[PopularService, Depends()],
    data: Annotated[PopularRequest, Depends()]
) -> List[PopularResponse]:
    """
查看热门搜索词列表。
-使用“实时”（TTLCache）缓存进行查询。
    """
    return service.fetch_populars(data.limit, data.day)


@router.post("/populars",
             summary="注册热门搜索词",
             responses={**response_409, **response_422, **response_500})
async def create_popular(
    request: Request,
    service: Annotated[PopularServiceAPI, Depends()],
    data: CreatePopularRequest
) -> MessageResponse:
    """
注册热门搜索词。
    """
    service.create_popular(request, data.fields, data.word)

    return {
        "message": "已注册一个热门搜索词."
    }


# @router.delete("/populars",
#                 summary="删除热门搜索词",
#                 responses={**response_422, **response_500})
# async def delete_popular(
#     service: Annotated[PopularServiceAPI, Depends()],
#     base_date: date = Query(default=date.today(),
#                             title="删除日期",
#                             description="删除基准日")
# ) -> MessageResponse:
#     """
#     按输入日期删除上一个日期注册的热门搜索词.
#     """
#     rowconut = service.delete_populars(base_date)
#     return {
#         "message": f"热门搜索词 {rowconut}案例已被删除。."
#     }
